package com.example.rocketmqproducer.controller;

import com.example.rocketmqproducer.producer.PayProducer;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;

/**
 * @author sherry
 * @description
 * @date Create in 2019/12/23
 * @modified By:
 */
@RestController
public class PayController {

    @Autowired
    private PayProducer payProducer;

    private static final String TOPIC_NAME = "pay_test_topic";

    @GetMapping("/api/v1/pay_cb")
    public Object callback(String text) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {

        Message message = new Message(TOPIC_NAME, "tagA", text.getBytes(Charset.forName("utf8")));

        SendResult send = payProducer.getProducer().send(message);

        return send;
    }
}